package chelydra.sharelydra.server.web.service;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import chelydra.sharelydra.server.web.SLSServlet;
import chelydra.sharelydra.server.ejb.SLSNotification;
import chelydra.sharelydra.server.ejb.SLSUser;

/**
 *
 * @author scallion
 */
public class SLSRegisterServlet extends SLSServlet {

	public SLSRegisterServlet() {
		this.action_name = "register";
	}

	@Override
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
		this.initManager(request);

		if ((request.getParameter("login") !=null) && (request.getParameter("password") !=null) && (request.getParameter("nickname") !=null)
						 && (request.getParameter("lastName") !=null) && (request.getParameter("firstName") !=null) && (request.getParameter("email") !=null)) {
			try {
				SLSUser user = new SLSUser();
				user.setLogin(request.getParameter("login"));
				user.setPassword(request.getParameter("password"));
				user.setNickname(request.getParameter("nickname"));
				user.setFirstName(request.getParameter("firstName"));
				user.setLastName(request.getParameter("lastName"));
				user.setEmail(request.getParameter("email"));
				user.setIp("");
				user.setPort(0);
				user.setHideEmail(0);
				user.setHideInSearch(0);
				this.manager.register(user);

				user = this.initAttributes(request, response);

			} catch (Exception ex) {
				ex.printStackTrace(System.err);
				request.setAttribute("fail_case",ex.getMessage());
				this.rd = request.getRequestDispatcher("/WEB-INF/jsp/fail.jsp");
			}

		} else {
			request.setAttribute("fail_case","All fields are required");
			this.rd = request.getRequestDispatcher("/WEB-INF/jsp/fail.jsp");
		}

		this.rd.forward(request, response);
  }

}
